/*form 编辑*/
(function ($) {
    $.fn.yqFormEdit = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqFormEdit.methods[opts](this, param);
        }

        return this.each(function () {
            var $this = $(this);
            var opt;
            if ($this.data("yqtype") == "yqFormEdit") {
                var _opt = options($this);
                destroy($this);
                opt = $.extend(_opt, opts);
            }
            else {
                opt = $.extend({}, $.fn.yqFormEdit.defaults, opts);
                var $uniqueID = $this.attr("ID");
                if ($uniqueID === undefined || $uniqueID === null || $uniqueID == "") {
                    $uniqueID = $.getUniqueID();
                    $this.attr("ID", $uniqueID);
                }
                opt.addkey = $.getKeyAdd();
            }

            opt.loadui = false;
            opt.loadrow = false;

            $this.addClass(opt.addClass);

            //变量信息
            $this.data("formedit_opt", opt);
            $this.data("yqtype", "yqFormEdit");

            init_edit_before($this);
        });
    };

    $.fn.yqFormEdit.defaults = {
        ajaxUrl: "/_db.html",
        titles: [],
        model: null,
        action: "add",
        row: {},
        loadDefault: false,   //加载默认值
        isEdit: true,
        blankTag: "*",     //值不能为空，标题附加标记
        controlType: 1,    //默认显示类型 1-textbox模式  2-div模式
        removeUnUse: false, //是否移除未使用的字段
        addClass: "yqformedit",  //附加样式
        beforeBindingTitle: function (titles, callback) {
            callback(titles);
        }, //标题绑定前
        afterBindingTitle: function () { },   //标题绑定后
        beforeSetRow: function (row, callback) {
            callback(row);
        },  //数据加载前
        afterSetRow: function (row) { },  //数据加载后
        endEditing: function (row) {   //ok.....

        }
    };

    $.fn.yqFormEdit.methods = {
        options: function (jq) {
            return options($(jq[0]));
        },
        setRow: function (jq, row) {   //{"ui_type":2,"value":""}
            setRow($(jq[0]), row);
        },
        clear: function (jq) {   //{"ui_type":2,"value":""}
            setRow($(jq[0]), {});
        },
        getRow: function (jq) {
            return getRow($(jq[0]));
        },
        isEdit: function (jq, tf) {
            isEdit($(jq[0]), tf);
        },
        destroy: function (jq) {
            var $object = $(jq[0]);
            destroy($object);

            $object.removeData("formedit_opt");
            $object.removeData("yqtype");
        },
        setField: function (jq, para) {
            if (para === undefined) {
                return;
            }
            if (para.name === undefined) {
                return;
            }
            if (para.value === undefined) {
                return;
            }

            setfield(jq, para.name, para.value);
        }
    };

    function options($object) {
        return $object.data("formedit_opt");
    }

    function destroy($object) {
        var opt = options($object);
        var titles = opt.titles;
        for (var i = 0; i < titles.length; i++) {
            var title = titles[i];
            var txtid = "txt" + opt.addkey + "_" + title.name;
            var $txt = $object.find("#" + txtid);
            if ($txt.length > 0) {
                if (title.ui_type == 2) { //textbox
                    $txt.textbox("destroy");
                }
                else if (title.ui_type == 3) { //checkbox
                    //$txt.checkbox("destroy");
                }
                else if (title.ui_type == 4) { //datebox
                    $txt.datebox("destroy");
                }
                else if (title.ui_type == 5) { //richbox

                }
                else if (title.ui_type == 6) { //Combobox
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        //$txt.find("[radiobuttonname='" + txtid + "']").radiobutton("destroy");

                    } else {
                        $txt.combobox("destroy");
                    }
                }
                else if (title.ui_type == 7) { //foreignkey
                    $txt.yqForeignKey("destroy");
                }
                else if (title.ui_type == 8) { //树形列表
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        //$txt.find("[radiobuttonname='" + txtid + "']").radiobutton("destroy");
                    } else {
                        $txt.combobox("destroy");
                    }
                }
                else if (title.ui_type == 9) { //NumberBox
                    //$txt.numberbox("destroy");
                }
                else if (title.ui_type == 10) { //NumberSpinner
                    //$txt.numberspinner("destroy");
                }
                else if (title.ui_type == 11) { //TimeSpinner
                    $txt.timespinner("destroy");
                }
                else if (title.ui_type == 12) { //DateTimeBox
                    $txt.datetimebox("destroy");
                }
                else if (title.ui_type == 13) { //NCheckBox 多选 
                    //$txt.checkbox("destroy");
                }
                else if (title.ui_type == 14) { //NForeignKey 多选外键
                    $txt.yqNForeignKey("destroy");
                }
                else if (title.ui_type == 15) { //TbEdit 表格数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        $txt.yqTableEdit("destroy");
                    } else {
                        $txt.yqTextTable("destroy");
                    }
                }
                else if (title.ui_type == 16) {  //tree 数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        $txt.yqTreeEdit("destroy");
                    } else {
                        $txt.yqTextTree("destroy");
                    }
                }
                else if (title.ui_type == 17) {  //attr 属性数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        $txt.yqAttrEdit("destroy");
                    } else {
                        $txt.yqTextAttr("destroy");
                    }
                }
                else if (title.ui_type == 18) {  //VirtualUi 虚表模板数据
                    $txt.yqTextFilebox("destroy");
                }
                else if (title.ui_type == 19) {  //html
                    $txt.yqHtmlEdit("destroy");
                }
                else if (title.ui_type == 20) {  //CSV
                    $txt.yqTextCSV("destroy");
                }
                else if (title.ui_type == 21) {  //Func
                    $txt.yqTextFunc("destroy");
                }
            }
        }
        $object.empty();

        $object.removeData("formedit_opt");
        $object.removeData("yqtype");
    }

    function init_edit_before($object) {
        var opt = options($object);
        opt.beforeBindingTitle.call($object, opt.titles,
            function (titles) {
                opt.titles = titles;
                init_edit($object);
            });
    }

    function init_edit($object) {
        var opt = options($object);

        if (opt.titles.length == 0) {
            opt.titles = [{
                "name": "textbox",
                "ui_title": "Textbox",
                "fd_type": "nvarchar",
                "length": 50,
                "decimal": 0,
                "ui_type": 2,
                "ui_width": 90,
                "ui_visible": true,
                "ui_readonly": false,
                "ui_mask": {
                    "default": "默认值",
                    "min": 0,
                    "max": 50,
                    "blank": false,
                    "format": ""
                }
            }];
        }

        var titles = $.yqFmUiTitle.call($object, opt.titles);
        opt.titles = titles;

        if (opt.model === null || opt.model == "") {
            model = getTbModel($object, opt.titles);
        } else if (opt.model.substring(0, 1) == "[") {
            model = getTbModel($object, JSON.parse(opt.model));
        } else {
            model = opt.model;
        }

        opt.model = model;

        dipModel($object);
    }

    function getTbModel($object, titles) {
        var opt = options($object);

        var table_arr = new Array();

        table_arr.push('<table>');
        table_arr.push('  <tbody>');

        var two = 0;
        for (var i = 0; i < titles.length; i++) {
            var title = titles[i];

            if (!title.is_func && title.ui_visible) {
                two++;
                if (two == 1) {
                    table_arr.push('   <tr>');
                }

                table_arr.push('      <td>');
                table_arr.push('            <span id="lbl_' + title.name + '">' + title.ui_title + '</span>');
                table_arr.push('      </td>');

                table_arr.push('      <td>');
                var editid = "txt_" + title.name;
                if (title.ui_type == 1) {
                    table_arr.push("<span id=\"" + editid + "\"></span>");
                }
                else if (title.ui_type == 2) { //textbox
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\"  id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 3) { //checkbox
                    table_arr.push("<input type=\"checkbox\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 4) { //datebox
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 5) { //richbox
                    table_arr.push("<TEXTAREA style=\"width: 200px;height:80px;\" id=\"" + editid + "\" style=\"HEIGHT: 50px; WIDTH: 330px\"></TEXTAREA>");
                }
                else if (title.ui_type == 6) { //Combobox
                    if (opt.controlType == 1) {
                        table_arr.push("<select style=\"width: 200px;\" id=\"" + editid + "\"></select>");
                    } else {
                        table_arr.push("<div id=\"" + editid + "\"></div>");
                    }
                }
                else if (title.ui_type == 7) { //foreignkey
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 8) { //树形列表
                    if (opt.controlType == 1) {
                        table_arr.push("<select style=\"width: 200px;\" id=\"" + editid + "\"></select>");
                    } else {
                        table_arr.push("<div id=\"" + editid + "\"></div>");
                    }
                }
                else if (title.ui_type == 9) { //NumberBox
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 10) { //NumberSpinner
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 11) { //TimeSpinner
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 12) { //DateTimeBox
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 13) { //NCheckBox 多选 
                    if (opt.controlType == 1) {
                        table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                    } else {
                        table_arr.push("<div id=\"" + editid + "\"></div>");
                    }
                }
                else if (title.ui_type == 14) { //NForeignKey 多选外键
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 15) { //TbEdit 表格数据
                    if (opt.controlType == 1) {
                        table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                    } else {
                        table_arr.push("<div id=\"" + editid + "\"></div>");
                    }
                }
                else if (title.ui_type == 16) {  //tree 数据
                    if (opt.controlType == 1) {
                        table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                    } else {
                        table_arr.push("<div id=\"" + editid + "\"></div>");
                    }
                }
                else if (title.ui_type == 17) {  //attr 属性数据
                    if (opt.controlType == 1) {
                        table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                    } else {
                        table_arr.push("<div id=\"" + editid + "\"></div>");
                    }
                }
                else if (title.ui_type == 18) {  //file 文件
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 19) {  //html
                    table_arr.push('<div id="' + editid + '" style="width: 300px;height:300px;"></div>');
                }
                else if (title.ui_type == 20) {  //CSV
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else if (title.ui_type == 21) {  //Func
                    table_arr.push("<input style=\"width: 200px;\" type=\"text\" id=\"" + editid + "\"/>");
                }
                else {
                    table_arr.push("&nbsp;");
                }
                table_arr.push('     </td>');
                if (two == 2) {
                    table_arr.push('   </tr>');
                    two = 0;
                }
            }
        }

        if (two == 1) {
            table_arr.push('     <td>&nbsp;</td>');
            table_arr.push('     <td>&nbsp;</td>');
            table_arr.push('   </tr>');
        }

        table_arr.push('  </tbody>');
        table_arr.push('</table>');

        var model = table_arr.join("");
        console.log(model);
        return model;
    }

    function dipModel($object) {
        var opt = options($object);
        $object.empty();
        var model = opt.model;
        model = model.replace(/lbl_/g, "lbl" + opt.addkey + "_");
        model = model.replace(/txt_/g, "txt" + opt.addkey + "_");
        $object.append("<form>" + model + "</form>");

        var titles = opt.titles;
        var defaultRow = {};
        for (var i = 0; i < titles.length; i++) {
            var title = titles[i];

            if (typeof title.ui_mask.onChange != "function") {
                title.ui_mask.onChange = function (target, value) { };
            }

            var $lbl = $object.find("#lbl" + opt.addkey + "_" + title.name);
            if ($lbl.length > 0) {
                if (title.ui_type == 3 || title.ui_mask.blank) {
                    $lbl.html(title.ui_title);
                } else {
                    $lbl.html(opt.blankTag + title.ui_title);
                }
            }

            var txtid = "txt" + opt.addkey + "_" + title.name;
            var $txt = $object.find("#" + txtid);
            if ($txt.length > 0) {
                var isEdit = opt.isEdit;
                if (title.ui_readonly) {
                    isEdit = false;
                }

                if (title.ui_type == 1) {
                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 2) { //textbox
                    $txt.textbox({
                        disabled: !isEdit,
                        onChange_: title.ui_mask.onChange,
                        onChange: function (newValue, oldValue) {
                            if (opt.loadui && !opt.loadrow) {
                                opt.endEditing.call($object, getRow($object));

                                var opt_ = $(this).textbox("options");
                                opt_.onChange_.call($(this), $object, newValue);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 3) { //checkbox
                    $txt.checkbox({
                        disabled: !isEdit,
                        onChange_: title.ui_mask.onChange,
                        onChange: function (checked) {
                            if (opt.loadui && !opt.loadrow) {
                                opt.endEditing.call($object, getRow($object));

                                var opt_ = $(this).checkbox("options");
                                opt_.onChange_.call($(this), $object, checked);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 4) { //datebox
                    $txt.datebox({
                        disabled: !isEdit,
                        format: title.ui_mask.format,
                        formatter: function (date) {
                            if (date === null) {
                                return "";
                            }
                            else {
                                return $.formatterdatebox(date, $(this).datebox("options").format);
                            }
                        },
                        parser: function (s) {
                            if (s === undefined || s == "" || s === null) {
                                return null;
                            }
                            return $.convert_todate(s);
                        },
                        onChange_: title.ui_mask.onChange,
                        onChange: function (newValue, oldValue) {
                            if (opt.loadui && !opt.loadrow) {
                                opt.endEditing.call($object, getRow($object));

                                var opt_ = $(this).textbox("options");
                                opt_.onChange_.call($(this), $object, newValue);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 5) { //richbox
                    $txt.yqCodeEdit({
                        types: ["base"],
                        value: "",
                        isEdit: isEdit,
                        onChange_: title.ui_mask.onChange,
                        onChange: function (val) {
                            if (opt.loadui && !opt.loadrow) {
                                opt.endEditing.call($object, getRow($object));

                                var opt_ = $(this).yqCodeEdit("options");
                                opt_.onChange_.call($(this), $object, val);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 6) { //Combobox
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        var gdata = [];
                        var lwidth = 30;
                        for (let j = 0; j < title.ui_mask.lists.length; j++) {
                            gdata.push({
                                value: title.ui_mask.lists[j].no,
                                label: title.ui_mask.lists[j].name
                            });

                            var lwidth_ = 11 * $.yqGetStrLen(title.ui_mask.lists[j].name);
                            if (lwidth_ > lwidth) {
                                lwidth = lwidth_;
                            }
                        }
                        gdata.push({ value: (title.ui_mask.type == "string" ? "" : -1), label: $.fn.yqUiExtText.ComboboxBlankText2 });

                        $txt.radiogroup({
                            name: txtid,
                            data: gdata,
                            labelWidth: lwidth + 10,
                            onChange_: title.ui_mask.onChange,
                            type: title.ui_mask.type,
                            onChange: function (newValue) {
                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));

                                    var opt_ = $(this).radiogroup("options");
                                    opt_.onChange_.call($(this), $object, newValue);
                                }
                            }
                        });

                        if (isEdit) {
                            $txt.find("[radiobuttonname='" + txtid + "']").radiobutton("enable");
                        } else {
                            $txt.find("[radiobuttonname='" + txtid + "']").radiobutton("disable");
                        }
                    } else {
                        var lists = [];
                        for (let j = 0; j < title.ui_mask.lists.length; j++) {
                            lists.push($.extend({}, title.ui_mask.lists[j]));
                        }
                        lists.unshift({ no: (title.ui_mask.type == "string" ? "" : -1), name: $.fn.yqUiExtText.ComboboxBlankText });
                        $txt.combobox({
                            data: lists,
                            value: -1,
                            valueField: "no",
                            textField: "name",
                            editable: false,
                            disabled: !isEdit,
                            panelHeight: 'auto',
                            panelMaxHeight: 154,
                            onChange_: title.ui_mask.onChange,
                            type: title.ui_mask.type,
                            onChange: function (newValue, oldValue) {
                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));

                                    var opt_ = $(this).combobox("options");
                                    opt_.onChange_.call($(this), $object, newValue);
                                }
                            }
                        });
                    }

                    if (!(title.ui_mask.default === null || title.ui_mask.default == -1)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 7) { //foreignkey
                    $txt.yqForeignKey({
                        ajaxUrl: opt.ajaxUrl,
                        panelWidth: title.ui_mask.panelWidth,
                        panelHeight: title.ui_mask.panelHeight,

                        displayType: title.ui_mask.displayType,
                        dbname: title.ui_mask.dbName,
                        tbname: title.ui_mask.tbName,
                        tempno: title.ui_mask.tempNo,
                        addWhere: title.ui_mask.addWhere,
                        order: title.ui_mask.order,

                        defaultValue: null,

                        isPage: title.ui_mask.isPage,
                        pageSize: title.ui_mask.pageSize,
                        pageNo: 1,
                        pageList: title.ui_mask.pageList === undefined ? [5, 10, 20, 30, 40, 50] : title.ui_mask.pageList,

                        valueMember: title.ui_mask.valueMember,
                        displayMember: title.ui_mask.displayMember,
                        mapping: title.ui_mask.mapping,

                        titles: [],

                        expandMember: title.ui_mask.expandMember === undefined ? null : title.ui_mask.expandMember,
                        seekNo: title.ui_mask.seekNo,
                        seekModeW: title.ui_mask.seekModeW,
                        seekModeH: title.ui_mask.seekModeH,
                        enable: isEdit,
                        onChange_: title.ui_mask.onChange,
                        onClickRow: function (rowIndex, rowData) {
                            if (opt.loadui && !opt.loadrow) {
                                var opt_ = $(this).yqForeignKey("options");
                                if (opt_.mapping.length > 0) {
                                    for (var j = 0; j < opt_.mapping.length; j++) {
                                        var val = null;
                                        if (!(rowData === null)) {
                                            val = rowData[opt_.mapping[j].from];
                                            if (val === undefined) {
                                                val = null;
                                            }
                                        }

                                        setfield($object, opt_.mapping[j].to, val);
                                    }
                                }

                                opt.endEditing.call($object, getRow($object));

                                opt_.onChange_.call($(this), $object, rowData);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 8) { //树形列表                   
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        var gdata = [];
                        var lwidth = 30;
                        if (title.ui_mask.level == 1) {
                            for (let j = 0; j < title.ui_mask.trees.length; j++) {
                                gdata.push({
                                    value: title.ui_mask.trees[j].no,
                                    label: title.ui_mask.trees[j].name,
                                    subs: title.ui_mask.trees[j].subs
                                });

                                var lwidth_ = 11 * $.yqGetStrLen(title.ui_mask.trees[j].name);
                                if (lwidth_ > lwidth) {
                                    lwidth = lwidth_;
                                }
                            }
                        }
                        gdata.push({ value: -1, label: $.fn.yqUiExtText.ComboboxBlankText2 });
                        var opts = {
                            name: txtid,
                            data: gdata,
                            labelWidth: lwidth + 10,
                            onChange_: title.ui_mask.onChange,
                        };
                        if (!(title.ui_mask.subfield === null || title.ui_mask.subfield == "")) {
                            opts.subfield = title.ui_mask.subfield;
                            opts.onChange = function (value) {
                                var txt_subid = "txt" + opt.addkey + "_" + $(this).radiogroup("options").subfield;
                                var $txt_sub = $object.find("#" + txt_subid);
                                if ($txt_sub.length > 0) {
                                    var lwidth = 30;
                                    var sub_gdata = [];
                                    if (value != -1) {
                                        var t_data = $(this).radiogroup("options").data;
                                        //console.log(t_data);
                                        for (let j = 0; j < t_data.length; j++) {
                                            if (value == t_data[j].value) {
                                                if (typeof t_data[j].subs != "undefined") {
                                                    for (var j2 = 0; j2 < t_data[j].subs.length; j2++) {
                                                        sub_gdata.push({
                                                            value: t_data[j].subs[j2].no,
                                                            label: t_data[j].subs[j2].name,
                                                            subs: t_data[j].subs[j2].subs
                                                        });

                                                        var lwidth_ = 11 * $.yqGetStrLen(t_data[j].subs[j2].name);
                                                        if (lwidth_ > lwidth) {
                                                            lwidth = lwidth_;
                                                        }
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    sub_gdata.push({ value: -1, label: $.fn.yqUiExtText.ComboboxBlankText2 });
                                    //console.log($txt_sub,sub_gdata);

                                    $txt_sub.radiogroup({
                                        name: txt_subid,
                                        data: sub_gdata,
                                        labelWidth: lwidth + 10
                                    });

                                    var $sub_radios = $txt_sub.find("[radiobuttonname='" + txt_subid + "']");
                                    for (let j = 0; j < $sub_radios.length; j++) {
                                        var $sub_radio = $($sub_radios[j]);
                                        //console.log("sub_radio", $sub_radio.radiobutton("options"));
                                        if ($sub_radio.radiobutton("options").item.value == -1) {
                                            $sub_radio.radiobutton("check");
                                        }
                                    }
                                }
                                //console.log(value, $txt_sub);

                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));

                                    var opt_ = $(this).radiogroup("options");
                                    opt_.onChange_.call($(this), $object, value);
                                }
                            }
                        } else {
                            opts.onChange = function (value) {
                                if (opt.loadui && !opt.loadrow) {
                                    var opt_ = $(this).radiogroup("options");
                                    opt_.onChange_.call($(this), $object, value);
                                }
                            }
                        }

                        $txt.radiogroup(opts);

                        if (isEdit) {
                            $txt.find("[radiobuttonname='" + txtid + "']").radiobutton("enable");
                        } else {
                            $txt.find("[radiobuttonname='" + txtid + "']").radiobutton("disable");
                        }
                    } else {
                        var lists = [];
                        if (title.ui_mask.level == 1) {
                            for (let j = 0; j < title.ui_mask.trees.length; j++) {
                                lists.push($.extend({}, title.ui_mask.trees[j]));
                            }
                        }

                        lists.unshift({ no: -1, name: $.fn.yqUiExtText.ComboboxBlankText });
                        var opts = {
                            value: -1,
                            data: lists,
                            valueField: "no",
                            textField: "name",
                            panelHeight: 'auto',
                            panelMaxHeight: 154,
                            editable: false,
                            disabled: !isEdit,
                            onChange_: title.ui_mask.onChange,
                        };
                        if (!(title.ui_mask.subfield === null || title.ui_mask.subfield == "")) {
                            opts.subfield = title.ui_mask.subfield;
                            opts.onChange = function (newValue, oldValue) {
                                var $txt_sub = $object.find("#txt" + opt.addkey + "_" + $(this).combobox("options").subfield);
                                if ($txt_sub.length > 0) {
                                    var lists = [];
                                    if (newValue != -1) {
                                        var lists_ = $(this).combobox("getData");
                                        for (var j = 0; j < lists_.length; j++) {
                                            if (newValue == lists_[j].no) {
                                                if (typeof lists_[j].subs != "undefined") {
                                                    for (var j2 = 0; j2 < lists_[j].subs.length; j2++) {
                                                        lists.push($.extend({}, lists_[j].subs[j2]));
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    lists.unshift({ no: -1, "name": $.fn.yqUiExtText.ComboboxBlankText });
                                    $txt_sub.combobox("loadData", lists);
                                    $txt_sub.combobox("setValue", -1);
                                }

                                if (opt.loadui && !opt.loadrow) {
                                    var opt_ = $(this).combobox("options");
                                    opt_.onChange_.call($(this), $object, newValue);
                                }
                            };
                        } else {
                            opts.onChange = function (newValue, oldValue) {
                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));

                                    var opt_ = $(this).combobox("options");
                                    opt_.onChange_.call($(this), $object, newValue);
                                }
                            };
                        }

                        $txt.combobox(opts);
                    }

                    if (title.ui_mask.level == 1 && !(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default[0];
                        var level = 1;
                        var subfield = title.ui_mask.subfield;
                        while (!(subfield === null || subfield == "") &&
                            title.ui_mask.default.length >= level
                        ) {
                            for (var j = 0; j < titles.length; j++) {
                                if (titles[j].name == subfield) {
                                    defaultRow[subfield] = title.ui_mask.default[level];
                                    level++;
                                    subfield = titles[j].ui_mask.subfield;
                                    if (subfield === undefined) {
                                        subfield = "";
                                    }
                                }
                            }
                        }
                    }
                }
                else if (title.ui_type == 9) { //NumberBox
                    $txt.numberbox({
                        precision: title.ui_mask.precision,
                        disabled: !isEdit,
                        onChange_: title.ui_mask.onChange,
                        onChange: function (newValue, oldValue) {
                            if (opt.loadui && !opt.loadrow) {
                                opt.endEditing.call($object, getRow($object));

                                var opt_ = $(this).textbox("options");
                                opt_.onChange_.call($(this), $object, newValue);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 10) { //NumberSpinner
                    $txt.numberspinner({
                        disabled: !isEdit,
                        onChange_: title.ui_mask.onChange,
                        onChange: function (newValue, oldValue) {
                            if (opt.loadui && !opt.loadrow) {
                                opt.endEditing.call($object, getRow($object));

                                var opt_ = $(this).textbox("options");
                                opt_.onChange_.call($(this), $object, newValue);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 11) { //TimeSpinner
                    $txt.timespinner({
                        disabled: !isEdit,
                        onChange_: title.ui_mask.onChange,
                        onChange: function (newValue, oldValue) {
                            if (opt.loadui && !opt.loadrow) {
                                opt.endEditing.call($object, getRow($object));

                                var opt_ = $(this).textbox("options");
                                opt_.onChange_.call($(this), $object, newValue);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 12) { //DateTimeBox
                    $txt.datetimebox({
                        disabled: !isEdit,
                        showSeconds: title.ui_mask.seconds,
                        format: title.ui_mask.format,
                        formatter: function (date) {
                            if (date === null) {
                                return "";
                            }
                            else {
                                return $.formatterdatetimebox(date, $(this).datebox("options").format);
                            }
                        },
                        onChange_: title.ui_mask.onChange,
                        onChange: function (newValue, oldValue) {
                            if (opt.loadui && !opt.loadrow) {
                                opt.endEditing.call($object, getRow($object));

                                var opt_ = $(this).textbox("options");
                                opt_.onChange_.call($(this), $object, newValue);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 13) { //NCheckBox 多选 
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        var gdata = [];
                        var len_ = 5;
                        for (let j = 0; j < title.ui_mask.lists.length; j++) {
                            gdata.push({
                                value: title.ui_mask.lists[j].no,
                                label: title.ui_mask.lists[j].name
                            });

                            var lwidth_ = $.yqGetStrLen(title.ui_mask.lists[j].name);
                            if (len_ < lwidth_) {
                                len_ = lwidth_;
                            }
                        }

                        $txt.append(`<form><div></div></form>`);
                        $txt.find("form>div").checkgroup({
                            name: txtid,
                            data: gdata,
                            labelWidth: len_ * 10,
                            onChange_: title.ui_mask.onChange,
                            onChange: function (values) {
                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));

                                    var opt_ = $(this).checkgroup("options");
                                    opt_.onChange_.call($(this), $object, values);
                                }
                            }
                        });

                        if (isEdit) {
                            $txt.find("[checkboxname='" + txtid + "']").checkbox("enable");
                        } else {
                            $txt.find("[checkboxname='" + txtid + "']").checkbox("disable");
                        }
                    } else {
                        $txt.yqTextNCheckBox({
                            lists: title.ui_mask.lists,
                            value: null,
                            blank: true,
                            isEdit: isEdit,   //可编辑
                            editModeW: 400, //模态编辑器宽度 格式为 800（数字）
                            editModeH: 300, //模态编辑器高度 格式为 600（数字）
                            onChange_: title.ui_mask.onChange,
                            afterChange: function (value) {
                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));

                                    var opt_ = $(this).yqTextNCheckBox("options");
                                    opt_.onChange_.call($(this), $object, value);
                                }
                            },
                        });
                    }

                    if (!(title.ui_mask.default === null || title.ui_mask.default == -1)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 14) { //NForeignKey 多选外键
                    $txt.yqNForeignKey({
                        ajaxUrl: opt.ajaxUrl,
                        panelWidth: title.ui_mask.panelWidth,
                        panelHeight: title.ui_mask.panelHeight,

                        dbname: title.ui_mask.dbName,
                        tbname: title.ui_mask.tbName,
                        tempno: title.ui_mask.tempNo,
                        addWhere: title.ui_mask.addWhere,
                        order: title.ui_mask.order,

                        defaultValue: null,

                        isPage: title.ui_mask.isPage,
                        pageSize: title.ui_mask.pageSize,
                        pageNo: 1,
                        pageList: title.ui_mask.pageList === undefined ? [5, 10, 20, 30, 40, 50] : title.ui_mask.pageList,

                        valueMember: title.ui_mask.valueMember,
                        displayMember: title.ui_mask.displayMember,
                        mapping: title.ui_mask.mapping,

                        titles: [],

                        expandMember: title.ui_mask.expandMember === undefined ? null : title.ui_mask.expandMember,
                        seekNo: title.ui_mask.seekNo,
                        seekModeW: title.ui_mask.seekModeW,
                        seekModeH: title.ui_mask.seekModeH,
                        enable: isEdit,
                        onChange_: title.ui_mask.onChange,
                        onClickRow: function (rows) {
                            if (opt.loadui && !opt.loadrow) {
                                var opt_ = $(this).yqNForeignKey("options");
                                if (opt_.mapping.length > 0) {
                                    for (var j = 0; j < opt_.mapping.length; j++) {
                                        var val = null;
                                        if (!(rows === null)) {
                                            for (var i = 0; i < rows.length; i++) {
                                                var _val = rows[i][opt_.mapping[j].from];
                                                if (!(_val === undefined)) {
                                                    if (val === null) {
                                                        val = _val;
                                                    } else {
                                                        val += ("," + _val);
                                                    }
                                                }
                                            }
                                        }

                                        setfield($object, opt_.mapping[j].to, val);
                                    }
                                }

                                opt.endEditing.call($object, getRow($object));

                                opt_.onChange_.call($(this), $object, rows);
                            }
                        }
                    });

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 15) { //TbEdit 表格数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        $txt.yqTableEdit({
                            ajaxUrl: opt.ajaxUrl,
                            titles: title.ui_mask.param.titles,
                            rows: [],
                            isEdit: isEdit,
                            isAdd: isEdit,
                            isDel: isEdit,
                            // editModeW: title.ui_mask.editWidth,
                            // editModeH: title.ui_mask.editHeight,
                            onChange_: title.ui_mask.onChange,
                            endEditing: function (index, row) {
                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));

                                    var opt_ = $(this).yqTableEdit("options");
                                    var rows_ = $(this).datagrid("getRows");;
                                    opt_.onChange_.call($(this), $object, rows_);
                                }
                            },
                        });
                    } else {
                        $txt.yqTextTable({
                            ajaxUrl: opt.ajaxUrl,
                            titles: title.ui_mask.param.titles,
                            rows: [],
                            isEdit: isEdit,
                            isAdd: isEdit,
                            isDel: isEdit,
                            editModeW: title.ui_mask.editModeW,
                            editModeH: title.ui_mask.editModeH,
                            onChange_: title.ui_mask.onChange,
                            beforeAdd: function (rows, row, callback) {
                                callback(row);
                            },
                            afterChange: function (rows) {
                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));

                                    var opt_ = $(this).yqTextTable("options");
                                    opt_.onChange_.call($(this), $object, rows);
                                }
                            },
                        });
                    }

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 16) {  //tree 数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        $txt.yqTreeEdit({
                            ajaxUrl: opt.ajaxUrl,
                            displayMember: title.ui_mask.displayMember,          //显示字段
                            titles: title.ui_mask.param.titles,
                            data: [],
                            isEdit: isEdit,
                            isAdd: isEdit,
                            isDel: isEdit,
                            // editModeW: title.ui_mask.editWidth,
                            // editModeH: title.ui_mask.editHeight,
                            onChange_: title.ui_mask.onChange,
                            endEditing: function (index, row) {
                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));

                                    var opt_ = $(this).yqTreeEdit("options");
                                    var rows_ = $(this).treegrid("getData");
                                    opt_.onChange_.call($(this), $object, rows_);
                                }
                            },
                        });
                    } else {
                        $txt.yqTextTree({
                            ajaxUrl: opt.ajaxUrl,
                            displayMember: title.ui_mask.displayMember,          //显示字段
                            titles: title.ui_mask.param.titles,
                            data: [],
                            isEdit: isEdit,
                            isAdd: isEdit,
                            isDel: isEdit,
                            editModeW: title.ui_mask.editModeW,
                            editModeH: title.ui_mask.editModeH,
                            onChange_: title.ui_mask.onChange,
                            afterChange: function (rows) {
                                if (opt.loadui && !opt.loadrow) {
                                    var opt_ = $(this).yqTextTree("options");
                                    opt_.onChange_.call($(this), $object, rows);
                                }
                            },
                        });
                    }

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
                else if (title.ui_type == 17) {  //attr 属性数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        var edit_type = title.ui_mask.editType;
                        if (!($txt.attr("edittype") === undefined)) {
                            edit_type = parseInt($txt.attr("edittype"));
                        }
                        var model_no = title.ui_mask.modelNo;
                        if (!($txt.attr("modelno") === undefined)) {
                            model_no = parseInt($txt.attr("modelno"));
                        }

                        if (edit_type == 1) {
                            $txt.yqAttrEdit({
                                ajaxUrl: opt.ajaxUrl,
                                titles: title.ui_mask.param.titles,
                                loadDefault: title.ui_mask.loadDefault,   //加载默认值
                                isEdit: isEdit,
                                // editModeW: title.ui_mask.editWidth,
                                // editModeH: title.ui_mask.editHeight,
                                onChange_: title.ui_mask.onChange,
                                endEditing: function (index, row) {   //ok.....
                                    if (opt.loadui && !opt.loadrow) {
                                        opt.endEditing.call($object, getRow($object));

                                        var opt_ = $(this).yqAttrEdit("options");
                                        opt_.onChange_.call($(this), $object, row);
                                    }
                                },
                                beforeAdd: function (rows, row, callback) { //ok....
                                    callback(row);
                                },
                            });
                        } else {
                            var model_;
                            for (let i_ = 0; i_ < title.ui_mask.param.model.length; i_++) {
                                const el_ = title.ui_mask.param.model[i_];
                                if (el_.no == model_no) {
                                    model_ = el_.model;
                                    break;
                                }
                            }
                            if (model_ === undefined) {
                                model_ = JSON.stringify(title.ui_mask.param.titles);
                            }
                            $txt.yqFormEdit({
                                ajaxUrl: opt.ajaxUrl,
                                titles: title.ui_mask.param.titles,
                                model: model_,
                                row: {},
                                loadDefault: title.ui_mask.loadDefault,   //加载默认值
                                isEdit: isEdit,
                                onChange_: title.ui_mask.onChange,
                                endEditing: function (row) {   //ok.....
                                    if (opt.loadui && !opt.loadrow) {
                                        opt.endEditing.call($object, getRow($object));

                                        var opt_ = $(this).yqFormEdit("options");
                                        opt_.onChange_.call($(this), $object, row);
                                    }
                                },
                            });
                        }

                    } else {
                        var model_;
                        for (let i_ = 0; i_ < title.ui_mask.param.model.length; i_++) {
                            const el_ = title.ui_mask.param.model[i_];
                            if (el_.no == model_no) {
                                model_ = el_.model;
                                break;
                            }
                        }
                        if (model_ === undefined) {
                            model_ = JSON.stringify(title.ui_mask.param.titles);
                        }

                        $txt.yqTextAttr({
                            ajaxUrl: opt.ajaxUrl,
                            editType: title.ui_mask.editType,  //1:属性编辑模式  2:form编辑模式
                            titles: title.ui_mask.param.titles,  //数据列配置对象
                            model: model_,
                            row: {},
                            loadDefault: false,
                            isEdit: isEdit,
                            editModeW: title.ui_mask.editModeW,
                            editModeH: title.ui_mask.editModeH,
                            onChange_: title.ui_mask.onChange,
                            beforeChange: function (row, callback) {    //弹出框中的事件
                                callback(true, row);
                            },
                            afterChange: function (row) { //修改后
                                if (opt.loadui && !opt.loadrow) {
                                    opt.endEditing.call($object, getRow($object));
                                    var opt_ = $(this).yqTextAttr("options");
                                    opt_.onChange_.call($(this), $object, row);
                                }
                            },
                        });
                    }

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                } else if (title.ui_type == 18) {  //file 文件
                    var opt_ = {
                        storageType: title.ui_mask.storageType,
                        storageName: title.ui_mask.storageName,
                        accept: title.ui_mask.accept,
                        chunk: title.ui_mask.chunk,
                        directup: title.ui_mask.directup,
                        isEdit: isEdit,   //可编辑
                    };
                    if (title.ui_mask.storageType == 1 && typeof opt.dbname == "string") {
                        opt_.storageName = opt.dbname;
                    }
                    $txt.yqTextFilebox(opt_);
                } else if (title.ui_type == 19) {  //html
                    var opt_ = {
                        storageType: title.ui_mask.storageType,
                        storageName: title.ui_mask.storageName,
                        accept: title.ui_mask.accept,
                        chunk: title.ui_mask.chunk,
                        directup: title.ui_mask.directup,
                        isEdit: isEdit,   //可编辑
                    };
                    if (title.ui_mask.storageType == 1 && typeof opt.dbname == "string") {
                        opt_.storageName = opt.dbname;
                    }
                    $txt.yqHtmlEdit(opt_);
                } else if (title.ui_type == 20) { //CSV
                    var _opt = title.ui_mask;
                    _opt.isEdit = isEdit;   //可编辑
                    _opt._afterChange = _opt.afterChange;
                    _opt.afterChange = function (value) { //修改后
                        if (opt.loadui && !opt.loadrow) {
                            opt.endEditing.call($object, getRow($object));
                            var _opt = $(this).yqTextCSV("options");
                            _opt._afterChange.call($(this), $object, value);
                        }
                    };
                    $txt.yqTextCSV(opt_);

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                } else if (title.ui_type == 21) { //Func
                    var _opt = title.ui_mask;
                    _opt.isEdit = isEdit;   //可编辑
                    _opt._afterChange = _opt.afterChange;
                    _opt.afterChange = function (value) { //修改后
                        if (opt.loadui && !opt.loadrow) {
                            opt.endEditing.call($object, getRow($object));
                            var _opt = $(this).yqTextFunc("options");
                            _opt._afterChange.call($(this), $object, value);
                        }
                    };
                    $txt.yqTextFunc(opt_);

                    if (!(title.ui_mask.default === null)) {
                        defaultRow[title.name] = title.ui_mask.default;
                    }
                }
            }
        }

        opt.loadui = true;

        if (Object.getOwnPropertyNames(opt.row).length > 0) {
            opt.afterBindingTitle.call($object);

            var row = opt.row;
            if (opt.loadDefault && opt.action == "add") {
                row = $.extend($.yqGetDefaultRow(opt.titles), row);
            }
            setRowRun($object, row, false);
        } else if (opt.loadDefault) {
            opt.afterBindingTitle.call($object);

            setRowRun($object, $.yqGetDefaultRow(opt.titles), false);
        } else {
            opt.afterBindingTitle.call($object);
        }
    }

    function setRow($object, row) {
        var opt = options($object);

        if (opt.loadui) {
            setRowRun($object, row, true);
        }
        else {
            setTimeout(function () {
                setRow($object, row);
            }, 500);
        }
    }

    //tf  替换当前row
    function setRowRun($object, row, tf) {
        var opt = options($object);

        if (tf === undefined) {
            tf = true;
        }

        opt.beforeSetRow.call($object, row, function (row_) {
            opt.loadrow = true;

            if (tf) {
                opt.row = row_;
            }

            setForienKeys($object, row_, function () {
                var titles = opt.titles;
                for (var i = 0; i < titles.length; i++) {
                    var title = titles[i];
                    var val = row_[title.name];
                    setFieldVal($object, title, val);
                }

                //数据加载后
                opt.afterSetRow.call($object, row_);

                opt.loadrow = false;
            });
        });

    }

    //设置行值
    function setfield($object, name, val) {
        var opt = options($object);

        if (val === undefined) {
            val = null;
        }
        if (val === null) {
            delete opt.row[name];
        } else {
            opt.row[name] = val;
        }

        for (var i = 0; i < opt.titles.length; i++) {
            if (opt.titles[i].name == name) {
                setFieldVal($object, opt.titles[i], val)
                break;
            }
        }
    }

    function setFieldVal($object, title, val) {
        var opt = options($object);
        var txtid = "txt" + opt.addkey + "_" + title.name;
        var $txt = $object.find("#" + txtid);
        if ($txt.length == 0) {
            return;
        }
        if (val === undefined || val === "") {
            val = null;
        }

        if (title.ui_type == 1) {
            if (val == null) {
                val = "";
            }
            $txt.html(val);
        } else if (title.ui_type == 2) { //textbox
            if (val == null) {
                val = "";
            }
            $txt.textbox("setValue", val);
        } else if (title.ui_type == 3) { //checkbox
            if (val == null) {
                val = false;
            } else {
                val = $.yqToBoolean(val);
            }
            if (val) {
                $txt.checkbox("check");
            } else {
                $txt.checkbox("uncheck");
            }
        } else if (title.ui_type == 4) { //datebox
            val = $.formatterdatebox(val, title.ui_mask.format);
            $txt.datebox("setValue", val);
        } else if (title.ui_type == 5) { //richbox
            if (val == null) {
                val = "";
            }
            $txt.yqCodeEdit("setValue", val);
        } else if (title.ui_type == 6) { //Combobox
            if (title.ui_mask.type == "string") {
                if (val === null) {
                    val = "";
                }
            } else {
                if (val === null) {
                    val = -1;
                } else {
                    val = $.yqToInt(val);
                }
            }

            if ($txt[0].tagName.toLowerCase() == "div") {
                $txt.radiogroup("setValue", val);
            } else {
                $txt.combobox("setValue", val);
            }
        } else if (title.ui_type == 7) { //foreignkey
            if (val == null) {
                $txt.yqForeignKey("clear");
            } else {
                $txt.yqForeignKey("setValue", val);
            }
        } else if (title.ui_type == 8) { //树形列表
            if (val == null) {
                val = -1;
            } else {
                val = $.yqToInt(val);
            }

            if ($txt[0].tagName.toLowerCase() == "div") {
                var $radios = $txt.find("[radiobuttonname='" + txtid + "']");
                for (let j = 0; j < $radios.length; j++) {
                    var $radio = $($radios[j]);
                    //console.log("sub_radio", $sub_radio.radiobutton("options"));
                    if ($radio.radiobutton("options").item.value == val) {
                        $radio.radiobutton("check");
                    }
                }
            } else {
                $txt.combobox("setValue", val);
            }
        } else if (title.ui_type == 9) { //NumberBox
            if (val == null) {
                $txt.numberbox("clear");
            } else {
                $txt.numberbox("setValue", val);
            }
        } else if (title.ui_type == 10) { //NumberSpinner
            //$txt.numberspinner("enable");
            if (val == null) {
                $txt.numberspinner("clear");
            } else {
                $txt.numberspinner("setValue", val);
            }
        } else if (title.ui_type == 11) { //TimeSpinner
            //$txt.timespinner("enable");
            if (val == null) {
                $txt.timespinner("clear");
            } else {
                $txt.timespinner("setValue", val);
            }
        } else if (title.ui_type == 12) { //DateTimeBox
            //$txt.datetimebox("enable");
            val = $.formatterdatetimebox(val, title.ui_mask.format);
            $txt.datetimebox("setValue", val);
        } else if (title.ui_type == 13) { //NCheckBox 多选   
            if ($txt[0].tagName.toLowerCase() == "div") {
                var val_ = [];
                if (!(val == "" || val === null || val === undefined)) {
                    var _val = val.split(",");
                    for (let j = 0; j < _val.length; j++) {
                        val_.push(parseInt(_val[j]));
                    }
                }
                $txt.find("form>div").checkgroup("setValue", val_);
            } else {
                if (val == null) {
                    val = "";
                }
                $txt.yqTextNCheckBox("setValue", val);
            }
        } else if (title.ui_type == 14) { //NForeignKey 多选外键
            if (val == null) {
                $txt.yqNForeignKey("clear");
            } else {
                $txt.yqNForeignKey("setValue", val);
            }
        } else if (title.ui_type == 15) { //TbEdit 表格数据
            if (val == null) {
                val = [];
            }
            if (typeof val == "string") {
                val = JSON.parse(val);
            }
            if ($txt[0].tagName.toLowerCase() == "div") {
                $txt.yqTableEdit("setRows", val);
            } else {
                $txt.yqTextTable("setRows", val);
            }
        } else if (title.ui_type == 16) {  //tree 数据
            if (val == null) {
                val = [];
            }
            if (typeof val == "string") {
                val = JSON.parse(val);
            }
            if ($txt[0].tagName.toLowerCase() == "div") {
                $txt.yqTreeEdit("setData", val);
            } else {
                $txt.yqTextTree("setData", val);
            }
        } else if (title.ui_type == 17) {  //attr 属性数据
            if (val == null) {
                val = [];
            }
            if (typeof val == "string") {
                val = JSON.parse(val);
            }
            if ($txt[0].tagName.toLowerCase() == "div") {
                var edit_type = title.ui_mask.editType;
                if (!($txt.attr("edittype") === undefined)) {
                    edit_type = parseInt($txt.attr("edittype"));
                }
                if (edit_type == 1) {
                    $txt.yqAttrEdit("setRow", val);
                } else {
                    $txt.yqFormEdit("setRow", val);
                }
            } else {
                $txt.yqTextAttr("setRow", val);
            }
        } else if (title.ui_type == 18) {  //file 文件
            $txt.yqTextFilebox("setData", val);
        } else if (title.ui_type == 19) {  //html
            $txt.yqHtmlEdit("setData", val);
        } else if (title.ui_type == 20) {  //CSV
            $txt.yqTextCSV("setValue", val);
        } else if (title.ui_type == 21) {  //Func
            $txt.yqTextFunc("setValue", val);
        }
    }

    function getRow($object) {
        var opt = options($object);
        var row = {};
        var titles = opt.titles;
        for (var i = 0; i < titles.length; i++) {
            var title = titles[i];
            var txtid = "txt" + opt.addkey + "_" + title.name;
            var $txt = $object.find("#" + txtid);
            if ($txt.length > 0) {
                var val = null;
                if (title.ui_type == 1) { //label
                    val = opt.row[title.name];
                    if (val === undefined) {
                        val = null;
                    }
                } else if (title.ui_type == 2) { //textbox
                    val = $txt.textbox("getValue");
                } else if (title.ui_type == 3) { //checkbox
                    val = $txt.next().hasClass("checkbox-checked");
                } else if (title.ui_type == 4) { //datebox
                    if ($txt.datebox("getText") != "") {
                        val = $txt.datebox("getValue");
                        val = $.convert_todate(val);
                    }
                } else if (title.ui_type == 5) { //richbox
                    val = $txt.yqCodeEdit("getValue");
                } else if (title.ui_type == 6) { //Combobox
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        val = $txt.radiogroup("getValue");
                    } else {
                        val = $txt.combobox("getValue");
                    }

                    if (title.ui_mask.type == "string") {
                        if (val == "") {
                            val = null;
                        }
                    } else {
                        val = $.yqToInt(val);
                        if (val == -1) {
                            val = null;
                        }
                    }
                } else if (title.ui_type == 7) { //foreignkey               
                    val = $txt.yqForeignKey("getValue");
                } else if (title.ui_type == 8) { //树形列表
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        val = $txt.radiogroup("getValue");
                    } else {
                        val = $txt.combobox("getValue");
                    }
                    val = $.yqToInt(val);
                    if (val == -1) {
                        val = null;
                    }
                } else if (title.ui_type == 9) { //NumberBox
                    if ($txt.numberbox("getText") != "") {
                        val = $txt.numberbox("getValue");
                        val = Number(val);
                        if (isNaN(val)) {
                            val = null;
                        }
                    }
                } else if (title.ui_type == 10) { //NumberSpinner
                    if ($txt.numberspinner("getText") != "") {
                        val = $txt.numberspinner("getValue");
                        val = $.yqToInt(val);
                    }
                } else if (title.ui_type == 11) { //TimeSpinner
                    val = $txt.timespinner("getValue");
                    if (val == "") {
                        val = null;
                    }
                } else if (title.ui_type == 12) { //DateTimeBox
                    if ($txt.datetimebox("getText") != "") {
                        val = $txt.datetimebox("getValue");
                        val = $.convert_todate(val);
                    }
                } else if (title.ui_type == 13) { //NCheckBox 多选 
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        var val = "";
                        var val_ = $txt.find("form>div").checkgroup("getValue");
                        for (let j = 0; j < val_.length; j++) {
                            val += (val == "" ? "" : ",") + val_[j];
                        }
                        if (val == "") {
                            val = null;
                        }
                    } else {
                        val = $txt.yqTextNCheckBox("getValue");
                    }
                } else if (title.ui_type == 14) { //NForeignKey 多选外键
                    val = $txt.yqNForeignKey("getValue");
                } else if (title.ui_type == 15) { //TbEdit 表格数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        val = $txt.yqTableEdit("getRows");
                    } else {
                        val = $txt.yqTextTable("getRows");
                    }
                } else if (title.ui_type == 16) {  //tree 数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        val = $txt.yqTreeEdit("getData");
                    } else {
                        val = $txt.yqTextTree("getData");
                    }
                } else if (title.ui_type == 17) {  //attr 属性数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        var edit_type = title.ui_mask.editType;
                        if (!($txt.attr("edittype") === undefined)) {
                            edit_type = parseInt($txt.attr("edittype"));
                        }
                        if (edit_type == 1) {
                            val = $txt.yqAttrEdit("getRow");
                        } else {
                            val = $txt.yqFormEdit("getRow");
                        }
                    } else {
                        val = $txt.yqTextAttr("getRow");
                    }
                } else if (title.ui_type == 18) {  //file 文件
                    val = $txt.yqTextFilebox("getData");
                } else if (title.ui_type == 19) {  //html
                    val = $txt.yqHtmlEdit("getData");
                } else if (title.ui_type == 20) {  //CSV
                    val = $txt.yqTextCSV("getValue");
                } else if (title.ui_type == 21) {  //Func
                    val = $txt.yqTextFunc("getValue");
                }

                row[title.name] = val;
            }
        }
        row = $.extend(opt.row, row);

        if (opt.removeUnUse) {
            var _row = {};
            for (let i = 0; i < opt.titles.length; i++) {
                var title = opt.titles[i];
                if (row.hasOwnProperty(title.name)) {
                    _row[title.name] = row[title.name];
                }
            }
            row = _row;
        }

        return row;
    }

    function isEdit($object, tf) {
        var opt = options($object);
        opt.isEdit = tf;
        var titles = opt.titles;
        for (var i = 0; i < titles.length; i++) {
            var title = titles[i];
            var txtid = "txt" + opt.addkey + "_" + title.name;
            var $txt = $object.find("#" + txtid);
            if ($txt.length > 0) {
                var isEdit = opt.isEdit;
                if (title.ui_readonly) {
                    isEdit = false;
                }

                if (title.ui_type == 1) {

                }
                else if (title.ui_type == 2) { //textbox
                    if (isEdit) {
                        $txt.textbox("enable");
                    } else {
                        $txt.textbox("disable");
                    }
                }
                else if (title.ui_type == 3) { //checkbox
                    if (isEdit) {
                        $txt.checkbox("enable");
                    } else {
                        $txt.checkbox("disable");
                    }
                }
                else if (title.ui_type == 4) { //datebox
                    if (isEdit) {
                        $txt.datebox("enable");
                    } else {
                        $txt.datebox("disable");
                    }
                }
                else if (title.ui_type == 5) { //richbox
                    $txt.yqCodeEdit("isEdit", isEdit);
                }
                else if (title.ui_type == 6) { //Combobox
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        if (isEdit) {
                            $txt.find("[radiobuttonname='" + txtid + "']").radiobutton("enable");
                        } else {
                            $txt.find("[radiobuttonname='" + txtid + "']").radiobutton("disable");
                        }
                    } else {
                        if (isEdit) {
                            $txt.combobox("enable");
                        } else {
                            $txt.combobox("disable");
                        }
                    }
                }
                else if (title.ui_type == 7) { //foreignkey
                    if (isEdit) {
                        $txt.yqForeignKey("enable");
                    } else {
                        $txt.yqForeignKey("disable");
                    }
                }
                else if (title.ui_type == 8) { //树形列表
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        if (isEdit) {
                            $txt.find("[radiobuttonname='" + txtid + "']").radiobutton("enable");
                        } else {
                            $txt.find("[radiobuttonname='" + txtid + "']").radiobutton("disable");
                        }
                    } else {
                        if (isEdit) {
                            $txt.combobox("enable");
                        } else {
                            $txt.combobox("disable");
                        }
                    }
                }
                else if (title.ui_type == 9) { //NumberBox
                    if (isEdit) {
                        $txt.numberbox("enable");
                    } else {
                        $txt.numberbox("disable");
                    }
                }
                else if (title.ui_type == 10) { //NumberSpinner
                    if (isEdit) {
                        $txt.numberspinner("enable");
                    } else {
                        $txt.numberspinner("disable");
                    }
                }
                else if (title.ui_type == 11) { //TimeSpinner
                    if (isEdit) {
                        $txt.timespinner("enable");
                    } else {
                        $txt.timespinner("disable");
                    }
                }
                else if (title.ui_type == 12) { //DateTimeBox
                    if (isEdit) {
                        $txt.datetimebox("enable");
                    } else {
                        $txt.datetimebox("disable");
                    }
                }
                else if (title.ui_type == 13) { //NCheckBox 多选 
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        if (isEdit) {
                            $txt.find("[checkboxname='" + txtid + "']").checkbox("enable");
                        } else {
                            $txt.find("[checkboxname='" + txtid + "']").checkbox("disable");
                        }
                    } else {
                        $txt.yqTextNCheckBox("isEdit", isEdit);
                    }
                }
                else if (title.ui_type == 14) { //NForeignKey 多选外键
                    if (isEdit) {
                        $txt.yqNForeignKey("enable");
                    } else {
                        $txt.yqNForeignKey("disable");
                    }
                }
                else if (title.ui_type == 15) { //TbEdit 表格数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        $txt.yqTableEdit("isEdit", isEdit);
                    } else {
                        $txt.yqTextTable("isEdit", isEdit);
                    }
                }
                else if (title.ui_type == 16) {  //tree 数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        $txt.yqTreeEdit("isEdit", isEdit);
                    } else {
                        $txt.yqTextTree("isEdit", isEdit);
                    }
                }
                else if (title.ui_type == 17) {  //attr 属性数据
                    if ($txt[0].tagName.toLowerCase() == "div") {
                        var edit_type = title.ui_mask.editType;
                        if (!($txt.attr("edittype") === undefined)) {
                            edit_type = parseInt($txt.attr("edittype"));
                        }
                        if (edit_type == 1) {
                            $txt.yqAttrEdit("isEdit", isEdit);
                        } else {
                            $txt.yqFormEdit("isEdit", isEdit);
                        }
                    } else {
                        $txt.yqTextAttr("isEdit", isEdit);
                    }
                }
                else if (title.ui_type == 18) {  //file 文件
                    if (isEdit) {
                        $txt.yqTextFilebox("enable");
                    } else {
                        $txt.yqTextFilebox("disable");
                    }
                }
                else if (title.ui_type == 19) {  //html
                    if (isEdit) {
                        $txt.yqHtmlEdit("enable");
                    } else {
                        $txt.yqHtmlEdit("disable");
                    }
                }
                else if (title.ui_type == 20) {  //CSV
                    $txt.yqTextCSV("isEdit",isEdit);
                }
                else if (title.ui_type == 21) {  //Func
                    $txt.yqTextFunc("isEdit",isEdit);
                }
            }
        }
    }

    function setForienKeys($object, row, callback) {
        var opt = options($object);
        var rows = [];
        rows.push(row);
        var opts = {
            "ajaxUrl": opt.ajaxUrl,
            "rows": rows,
            "titles": opt.titles,
            "fk_rows": opt.fk_rows === undefined ? [] : opt.fk_rows
        };

        $.yqGetForeignkeys(opts,
            function (fk_rows) {
                opt.fk_rows = fk_rows;
                callback();
            });
    }
})(jQuery);

